<template>
  <div class="modal">
    <div class="modal-overlay" @click="handleClose"></div>
    <div class="modal-content">
      <div class="modal-header">
        <h2>个人资料</h2>
        <button class="close-btn" @click="handleClose">×</button>
      </div>

      <div class="modal-body">
        <div v-if="error" class="error-message">
          {{ error }}
        </div>

        <form @submit.prevent="handleSubmit">
          <div class="form-group">
            <label>用户名</label>
            <input
              v-model="form.username"
              type="text"
              disabled
            />
            <small class="form-hint">用户名不可修改</small>
          </div>

          <div class="form-group">
            <label>邮箱</label>
            <input
              v-model="form.email"
              type="email"
              disabled
            />
            <small class="form-hint">邮箱不可修改</small>
          </div>

          <div class="form-group">
            <label>昵称</label>
            <input
              v-model="form.nickname"
              type="text"
              placeholder="请输入昵称"
              :disabled="isLoading"
            />
          </div>

          <div class="form-group">
            <label>头像URL</label>
            <input
              v-model="form.avatar_url"
              type="url"
              placeholder="https://example.com/avatar.jpg"
              :disabled="isLoading"
            />
          </div>

          <div class="form-actions">
            <button type="button" class="btn btn-secondary" @click="handleClose" :disabled="isLoading">
              取消
            </button>
            <button type="submit" class="btn btn-primary" :disabled="isLoading">
              {{ isLoading ? '保存中...' : '保存' }}
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useAuthStore } from '@/stores/auth';

const emit = defineEmits<{
  close: [];
}>();

const authStore = useAuthStore();

const isLoading = ref(false);
const error = ref<string | null>(null);

const form = ref({
  username: '',
  email: '',
  nickname: '',
  avatar_url: '',
});

onMounted(() => {
  if (authStore.user) {
    form.value = {
      username: authStore.user.username,
      email: authStore.user.email,
      nickname: authStore.user.nickname || '',
      avatar_url: authStore.user.avatar_url || '',
    };
  }
});

async function handleSubmit() {
  try {
    isLoading.value = true;
    error.value = null;
    
    await authStore.updateProfile({
      nickname: form.value.nickname,
      avatar_url: form.value.avatar_url,
    });
    
    handleClose();
  } catch (e) {
    // 错误已在store中处理
  } finally {
    isLoading.value = false;
  }
}

function handleClose() {
  emit('close');
}
</script>

<style lang="scss" scoped>
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: relative;
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;

  h2 {
    margin: 0;
    font-size: 1.5rem;
  }

  .close-btn {
    background: none;
    border: none;
    font-size: 2rem;
    cursor: pointer;
    color: #999;
    padding: 0;
    width: 30px;
    height: 30px;

    &:hover {
      color: #333;
    }
  }
}

.modal-body {
  padding: 20px;
}

.error-message {
  background: #fee;
  color: #c33;
  padding: 10px;
  border-radius: 6px;
  margin-bottom: 15px;
  font-size: 0.9rem;
}

.form-group {
  margin-bottom: 20px;

  label {
    display: block;
    margin-bottom: 5px;
    font-weight: bold;
    color: #333;
  }

  input {
    width: 100%;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 1rem;

    &:focus {
      outline: none;
      border-color: #4A90E2;
    }

    &:disabled {
      background: #f5f5f5;
      cursor: not-allowed;
    }
  }

  .form-hint {
    display: block;
    margin-top: 5px;
    color: #666;
    font-size: 0.85rem;
  }
}

.form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 30px;

  .btn {
    padding: 10px 30px;
  }
}
</style>
